package cn.oveay.classdesign.service.impl;

import cn.oveay.classdesign.bean.basebean.Option;
import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.dao.SubjectDao;
import cn.oveay.classdesign.dao.impl.SubjectDaoImpl;
import cn.oveay.classdesign.service.OptionService;
import cn.oveay.classdesign.service.RecordService;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.util.commons.DateFormatter;

import java.util.List;

public class SubjectServiceImpl implements SubjectService {
    private SubjectDao subjectDao = new SubjectDaoImpl();
    private OptionService optionService = new OptionServiceImpl();
    private RecordService recordService = new RecordServiceImpl();

    @Override
    public List<Subject> findAll() {
        List<Subject> subjectList = subjectDao.findAll();
        for(Subject subject : subjectList){
            List<Option> optionList = optionService.findBySubjectID(subject.getId());
            subject.setOptions(optionList);
            subject.setCount(recordService.findUserIdBySubjectId(subject.getId()));
            subject.setNumberView(optionList.size() + "");
            subject.setStartTimeView(DateFormatter.formatToDate(subject.getStarttime()));
            subject.setEndTimeView(DateFormatter.formatToDate(subject.getEndtime()));
        }
        return subjectList;
    }

    @Override
    public List<Subject> findByUserId(String userId) {
        List<Subject> subjectList = subjectDao.findByUserId(userId);
        for(Subject subject : subjectList){
            List<Option> optionList = optionService.findBySubjectID(subject.getId());
            subject.setOptions(optionList);
            subject.setCount(recordService.findUserIdBySubjectId(subject.getId()));
            subject.setNumberView(optionList.size() + "");
            subject.setStartTimeView(DateFormatter.formatToDate(subject.getStarttime()));
            subject.setEndTimeView(DateFormatter.formatToDate(subject.getEndtime()));
        }
        return subjectList;
    }

    @Override
    public Subject findById(String id) {
        Subject subject = subjectDao.findById(id);
        List<Option> optionList = optionService.findBySubjectID(subject.getId());
        subject.setOptions(optionList);
        subject.setCount(recordService.findUserIdBySubjectId(subject.getId()));
        subject.setNumberView(optionList.size() + "");
        subject.setStartTimeView(DateFormatter.formatToDate(subject.getStarttime()));
        subject.setEndTimeView(DateFormatter.formatToDate(subject.getEndtime()));
        return subject;
    }

    @Override
    public List<Subject> findByKey(String key) {
        List<Subject> subjectList = subjectDao.findByKey(key);
        for(Subject subject : subjectList){
            List<Option> optionList = optionService.findBySubjectID(subject.getId());
            subject.setOptions(optionList);
            subject.setCount(recordService.findUserIdBySubjectId(subject.getId()));
            subject.setNumberView(optionList.size() + "");
            subject.setStartTimeView(DateFormatter.formatToDate(subject.getStarttime()));
            subject.setEndTimeView(DateFormatter.formatToDate(subject.getEndtime()));
        }
        return subjectList;
    }

    @Override
    public void inseart(Subject subject) {
        subjectDao.inseart(subject);
        List<Option> list = subject.getOptions();
        if(list != null){
            for(Option option : list){
                optionService.inseart(option);
            }
        }
    }

    @Override
    public void update(Subject subject) {
        subjectDao.update(subject);
    }

    @Override
    public void delete(String id) {
        subjectDao.delete(id);
    }
}
